package com.shjn.squirrel.customView;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.shjn.squirrel.R;

/**
 * 返回与倒计时器控制面板
 */
public class BackAndTimerView extends RelativeLayout implements View.OnClickListener {

    /**
     * 返回按钮
     */
    private Button mBack;

    /**
     * 倒计时显示
     */
    private Button mTimer;

    /**
     * 定时时间
     */
    private int mSecondTime;

    /**
     * 返回事件回调接口对象
     */
    private OnBackListener mBackListener;

    /**
     * 定时完成回调接口对象
     */
    private OnTimerFinishListener mTimerFinishListener;

    /**
     * 倒计时器对象
     */
    private CountDownTimer mCountDownTimer;

    /**
     * 返回事件接口
     */
    public interface OnBackListener {
        void onBack();
    }

    /**
     * 倒计时完成事件接口
     */
    public interface OnTimerFinishListener {
        void onTimerFinish();
    }

    public BackAndTimerView(Context context) {
        super(context);
        initView();
    }

    public BackAndTimerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    /**
     * 加载布局文件，初始化相关控件
     */
    private void initView() {
        View backAndTimer = inflate(getContext(), R.layout.back_and_timer, this);
        mBack = (Button) backAndTimer.findViewById(R.id.btn_back);
        mTimer = (Button) backAndTimer.findViewById(R.id.btn_timer);
        mBack.setOnClickListener(this);
    }

    /**
     * 设置返回回调接口
     *
     * @param backListener
     * @return
     */
    public BackAndTimerView setOnBackListener(OnBackListener backListener) {
        mBackListener = backListener;
        return this;
    }

    /**
     * 设置倒计时时间完成回调接口
     *
     * @param timerFinishListener
     * @return
     */
    public BackAndTimerView setOnTimerFinishListener(OnTimerFinishListener timerFinishListener) {
        mTimerFinishListener = timerFinishListener;
        return this;
    }

    /**
     * 设置定时时间
     *
     * @param secondTime
     * @return
     */
    public BackAndTimerView setTime(int secondTime) {
        mSecondTime = secondTime;
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
        }
        mCountDownTimer = new MyCountDownTimer(mSecondTime * 1000 + 1050, 1000);
        return this;
    }

    /**
     * 设置控件显示和隐藏
     */
    public BackAndTimerView setVisibleState(Boolean isVisble) {
        if (isVisble) {
            setVisibility(VISIBLE);
        } else {
            setVisibility(INVISIBLE);
        }
        return this;
    }

    /**
     * 设置返回按钮显示或隐藏
     *
     * @param isVisble
     * @return
     */
    public BackAndTimerView setBackVisibleState(Boolean isVisble) {
        if (isVisble) {
            mBack.setVisibility(VISIBLE);
        } else {
            mBack.setVisibility(INVISIBLE);
        }
        return this;
    }

    /**
     * 设置返回使能
     */
    public BackAndTimerView setBackEnable(boolean isEnable) {
        if (isEnable) {
            mBack.setEnabled(true);
        } else {
            mBack.setEnabled(false);
        }
        return this;
    }

    /**
     * 开始倒计时
     */
    public void start() {
        if (mCountDownTimer != null) {
            mCountDownTimer.start();
        }
    }

    /**
     * 停止倒计时
     */
    public void stop() {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
        }
    }

    /**
     * 获取当前时间
     */
    public int getCurrentTime() {
        return Integer.parseInt(mTimer.getText().toString().substring(0,
                mTimer.getText().length() - 1));
    }

    /**
     * 按钮点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                if (mBackListener != null) {
                    mCountDownTimer.cancel();
                    mBackListener.onBack();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 倒计时器类
     */
    private class MyCountDownTimer extends CountDownTimer {
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long time) {
            int now = (int) time / 1000 - 1;
            if (now == 0 && mTimerFinishListener != null) {
                mTimerFinishListener.onTimerFinish();
            }
            mTimer.setText(now + "" + "秒");
        }

        @Override
        public void onFinish() {

        }
    }
}
